package cn.itcast.controller.system;

import cn.itcast.base.BaseController;
import cn.itcast.domain.system.Dept;
import cn.itcast.service.system.DeptService;
import cn.itcast.utils.commons.UtilFuns;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/system/dept")
public class DeptController extends BaseController {

    @Autowired
    private DeptService deptService;

    @RequestMapping(value = "/list",name = "部门的分页查询")
    public String findAll(@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "4") int pageSize) {
        PageInfo pageInfo = deptService.pageHelperFindAll(pageNum, pageSize, companyId);
        request.setAttribute("page",pageInfo);
        return "system/dept/dept-list";
        // List<Dept> list = deptService.findAll();
        //request.setAttribute("deptList",list);
        //return "/system/dept/dept-list";
       // return pageInfo;
    }


    @RequestMapping("/toAdd")
    public String toAdd() {
        //获取所有部门
        List<Dept> list = deptService.findAllDept(companyId);
        request.setAttribute("deptList",list);
        return "system/dept/dept-add";
    }

    @RequestMapping("/delete")
    public String delete(String id) {
        deptService.delete(id);
        return "redirect:/system/dept/list.do";
    }

    @RequestMapping(value = "/edit",name = "部门的新增页面")
    public String edit(Dept dept) {
        //设置部门id
        dept.setCompanyId(companyId);
        //设置部门名称
        dept.setCompanyName(companyName);
        if (UtilFuns.isEmpty(dept.getId())) {

            dept.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            deptService.save(dept);
        } else {
            deptService.updateDept(dept);
        }
        return "redirect:/system/dept/list.do";
    }

    @RequestMapping(value = "/toUpdate",name = "页面编辑页面")
    public String toUpdate(String id){
        //获取要修改的部门
        Dept dept = deptService.findById(id);

        List<Dept> list = deptService.findAllDept(companyId);
        request.setAttribute("dept",dept);
        request.setAttribute("deptList",list);
        return "system/dept/dept-update";
    }
}
